﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace FirstFloor.P2P
{
    /// <summary>
    /// Provides the list of participating users.
    /// </summary>
    public class P2PUserList
        : IEnumerable<P2PUser>
    {
        private List<P2PUser> users = new List<P2PUser>();

        /// <summary>
        /// Initializes a new instance of the <see cref="P2PUserList"/> class.
        /// </summary>
        internal P2PUserList()
        {
        }

        /// <summary>
        /// Adds a user.
        /// </summary>
        /// <param name="user">The user.</param>
        internal void Add(P2PUser user)
        {
            this.users.Add(user);

            if (user.IsMe) {
                this.Me = user;
            }
            if (user.IsInviter){
                this.Inviter = user;
            }
        }

        internal void Clear()
        {
            this.users.Clear();
        }

        /// <summary>
        /// Gets the count.
        /// </summary>
        /// <value>The count.</value>
        public int Count
        {
            get { return this.users.Count; }
        }

        /// <summary>
        /// Gets the <see cref="FirstFloor.P2P.P2PUser"/> at the specified index.
        /// </summary>
        /// <value></value>
        public P2PUser this[int index]
        {
            get { return this.users[index]; }
        }

        /// <summary>
        /// Gets the local user.
        /// </summary>
        /// <value>Me.</value>
        public P2PUser Me { get; private set; }

        /// <summary>
        /// Gets the inviter.
        /// </summary>
        /// <value>The inviter.</value>
        public P2PUser Inviter { get; private set; }

        #region IEnumerable<P2PUser> Members

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator<P2PUser> GetEnumerator()
        {
            return this.users.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator IEnumerable.GetEnumerator()
        {
            return this.users.GetEnumerator();
        }

        #endregion
    }
}
